package com.qkl.xiatang.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qkl.xiatang.app.dto.BusinessDTO;
import com.qkl.xiatang.app.dto.feed.FeedVO;
import com.qkl.xiatang.app.dto.feed.QueryFeedListOut;
import com.qkl.xiatang.app.dto.feed.StatisticsFeedInfoVO;
import com.qkl.xiatang.app.dto.log.JobLogTitleDTO;
import com.qkl.xiatang.app.entity.feed.*;
import com.qkl.xiatang.app.mapper.FeedAllocQtyMapper;
import com.qkl.xiatang.app.mapper.FeedManagerMapper;
import com.qkl.xiatang.app.mapper.ZooAllocDetaiMapper;
import com.qkl.xiatang.app.utils.AppSessionUtils;
import com.qkl.xiatang.app.utils.AppUtils;
import com.qkl.xiatang.app.utils.JobLogUtils;
import com.qkl.xiatang.app.utils.appenum.FeedTypeEnum;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class FeedManagerService {

    @Autowired
    private FeedManagerMapper feedMapper;

    @Autowired
    private FeedAllocQtyMapper feedAllocQtyMapper;

    @Autowired
    private ZooAllocDetaiMapper zooAllocDetaiMapper;

    /**
     * 查询饲料列表
     * @param companyId
     * @param type 1饲料，2动保
     * @return
     */
    public List<QueryFeedListOut> queryFeedList(String companyId, Integer type){
        return feedMapper.queryFeedList(companyId,type);
    }

    public FeedEntity queryFeed(String feedId){
        return feedMapper.selectById(feedId);
    }

    public List<QueryFeedListOut>  queryFeedList(String companyId, FeedTypeEnum typeEnum){
        Integer type = null;
        if(null != typeEnum){
            type = typeEnum.getType();
        }
        return this.queryFeedList(companyId,type);
    }

    /**
     * 查询饲料列表
     * @param typeEnum
     * @return
     */
    public BusinessDTO<Object> queryFeedList(FeedTypeEnum typeEnum){
        Map<String,Object> map = new HashMap<>();
        map.put("list",this.queryFeedList(AppSessionUtils.getCompanyId(),typeEnum));
        return BusinessDTO.sucessBusiness(map,"查询饲料");
    }

    /**
     * 查询库存
     * @return
     */
    public BusinessDTO<Object> queryStock(){
        String companyId = AppSessionUtils.getCompanyId();
        Map<String,Object> map = new HashMap<>();
        List<QueryFeedListOut> feedStock = feedMapper.queryFeedStock(companyId,FeedTypeEnum.SHRIMP.getType());
        List<QueryFeedListOut> zooStock = feedMapper.queryFeedStock(companyId,FeedTypeEnum.HEALTH.getType());
        //map.put("feedStock",this.queryFeedList(AppSessionUtils.getCompanyId(),FeedTypeEnum.SHRIMP));
        //map.put("zooStock",this.queryFeedList(AppSessionUtils.getCompanyId(),FeedTypeEnum.HEALTH));
        map.put("feedStock",feedStock);
        map.put("zooStock",zooStock);
        return BusinessDTO.sucessBusiness(map,"库存");
    }

    /**
     * 统计饲料、动保 喂料情况
     * @param pondId
     * @return
     */
    public BusinessDTO<Object> statisticsFeedInfo(String pondId){
        String companyId = AppSessionUtils.getCompanyId();
        List<StatisticsFeedInfoVO> statisticsList = new ArrayList<>();
        List<StatisticsFeedInfoVO> feedList = feedMapper.statisticsFeedFeedInfo(pondId,companyId);
        String feedInfo = StatisticsFeedInfoVO.showFeedInfo(feedList);
        if(ObjectUtils.isNotEmpty(feedList)){
            statisticsList.addAll(feedList);
        }
        List<StatisticsFeedInfoVO> zooList = feedMapper.statisticsZooFeedInfo(pondId,companyId);
        String zooInfo = StatisticsFeedInfoVO.showFeedInfo(zooList);
        if(ObjectUtils.isNotEmpty(zooList)){
            statisticsList.addAll(zooList);
        }
        StatisticsFeedInfoVO.InfoDTO infoDTO = StatisticsFeedInfoVO.findMinMaxDate(statisticsList);
        String dateInfo = StatisticsFeedInfoVO.showDate(infoDTO);

        Map<String,Object> infoMap = new HashMap<>();
        infoMap.put("feedInfo",feedInfo);
        infoMap.put("zooInfo",zooInfo);
        infoMap.put("dateInfo",dateInfo);

        List<Map<String,Object>> list = new ArrayList<>();
        list.add(infoMap);
        List<JobLogTitleDTO> jobLogTitle = JobLogUtils.statFeedTitle();

        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("jobLogTitle",jobLogTitle);
        return BusinessDTO.sucessBusiness(map,"统计饲料、动保 喂料情况");
    }

    public List<String> queryFeedImageTaskAll(AllocBaseEntity task){
        if(task instanceof FeedAllocDetailEntity){
            return this.queryFeedImageTaskAll(task.getCompanyId(),1,task.getAllocDate(),((FeedAllocDetailEntity)task).getSerialNo());
        }
        return this.queryFeedImageTaskAll(task.getCompanyId(),2,task.getAllocDate(),0);
    }

    public FeedVO queryFeedImageList(List<String> feedIdList){
        return this.queryFeedImageList(feedIdList,"");
    }

    public FeedVO queryFeedImageList(String feedId){
        return this.queryFeedImageList(null,feedId);
    }


    /**
     * 动保未喂料
     * @param companyId
     * @param allocDate
     * @return
     */
    public List<String> queryFeedImageZooUnfeed(String companyId,Date allocDate){
        return this.queryFeedImageUnfeedTask(companyId,2,allocDate,0);
    }
    /**
     * 饲料未喂料
     * @param companyId
     * @param allocDate
     * @param serialNo
     * @return
     */
    public List<String> queryFeedImageFeedUnfeed(String companyId,Date allocDate, Integer serialNo){
        return this.queryFeedImageUnfeedTask(companyId,1,allocDate,serialNo);
    }

    /**
     * 未查料台
     * @param companyId
     * @param allocDate
     * @param serialNo
     * @return
     */
    public List<String> queryFeedImageUncheck(String companyId,Date allocDate, Integer serialNo){
        return this.queryFeedImageTaskAll(companyId,1,allocDate,serialNo,"check_time");
    }

    /**
     * 查询动保饲料id
     * @param companyId
     * @param allocDate
     * @return
     */
    public List<String> queryZooFeedIdList(String companyId,Date allocDate){
        return feedMapper.queryZooFeedIdList(companyId,allocDate);
    }

    public List<String> getFeedIdList(AllocBaseEntity task){
        List<String> feedIdList = new ArrayList<>();
        if(task instanceof FeedAllocDetailEntity){
            List<FeedAllocQtyEntity> qtyEntityList = feedAllocQtyMapper.selectList(new QueryWrapper<FeedAllocQtyEntity>().eq("detail_id",task.getId()));
            if(ObjectUtils.isNotEmpty(qtyEntityList)){
                for (FeedAllocQtyEntity qtyEntity : qtyEntityList){
                    feedIdList.add(qtyEntity.getFeedId());
                }
            }
        }else {
            if(StringUtils.isEmpty(task.getFeedId())){
                List<ZooAllocDetailEntity> detailEntities = zooAllocDetaiMapper.selectList(new QueryWrapper<ZooAllocDetailEntity>().eq("company_id",task.getCompanyId()).eq("alloc_date",task.getAllocDate()));
                if(ObjectUtils.isNotEmpty(detailEntities)){
                    for (ZooAllocDetailEntity detail : detailEntities){
                        feedIdList.add(detail.getFeedId());
                    }
                }
            }else {
                feedIdList.add(task.getFeedId());
            }
        }
        feedIdList = AppUtils.stringsDistinct(feedIdList);
        return feedIdList;
    }


    private List<String> queryFeedImageUnfeedTask(String companyId, Integer type,Date allocDate, Integer serialNo){
        return this.queryFeedImageTaskAll(companyId,type,allocDate,serialNo,"feed_time");
    }


    private List<String> queryFeedImageTaskAll(String companyId, Integer type,Date allocDate, Integer serialNo){
        return this.queryFeedImageTaskAll(companyId,type,allocDate,serialNo,null);
    }

    private List<String> queryFeedImageTaskAll(String companyId, Integer type,Date allocDate, Integer serialNo,String timeFieldName){
        return feedMapper.queryFeedImageTaskAll(companyId,type,allocDate,serialNo,timeFieldName);
    }

    private FeedVO queryFeedImageList( List<String> feedIdList,  String feedId){
        FeedVO vo = new FeedVO();
        vo.setEntityList(feedMapper.queryFeedImageList(feedIdList,feedId));
        return vo;
    }
}
